package com.byxy.luntan.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.byxy.luntan.entity.Forum;
import com.byxy.luntan.entity.Reply;
import com.byxy.luntan.entity.Tab;
import com.byxy.luntan.entity.Tip;
import com.byxy.luntan.entity.User;
import com.byxy.luntan.service.ForumService;
import com.byxy.luntan.service.ReplyService;
import com.byxy.luntan.service.TabService;
import com.byxy.luntan.service.TipService;
import com.byxy.luntan.vo.TipVO;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class TabController {

//	@Slf4j  == 以下代码  变量log
	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired private ForumService forumService;
	@Autowired private TabService tabService;
	@Autowired private TipService tipService;
	
	
	@Autowired
	private ReplyService replyService;
	
	@GetMapping("toPublishTipPage.do")
	public String add(Model model) {
			List<Forum> forums  = forumService.findAll();
			model.addAttribute("forums",forums);
			
			List<Tab> tabs =tabService.findOut();
			model.addAttribute("tabs",tabs);
			
		return "publishTip";
	}
	
	

	@PostMapping("publishNewTip.do")
	public String doAdd(Tip tip, String selectedForumId,HttpSession session) {
		logger.info("TipContent = "+ tip.getTipContent());
		logger.info("tipTitle = "+ tip.getTipTitle());
		logger.info("TabId = "+ tip.getTabId());
		logger.info("selectedForumId = "+ selectedForumId );
		User u = (User)session.getAttribute("USER");
		tip.setUserId(u.getUserId());
//		tip.setUserId(u.getUserId());
		tip.setTipClick(0);
		tip.setTipIsdeleted(0);
		tip.setTipIsKnot(0);
		tip.setTipModifyTime(new Date());
		tip.setTipPublishTime(new Date());
		tip.setTipReplies(0);
		tipService.add(tip);
		return "redirect:toMainPage.do";
	}

	
	@GetMapping("showTip.do")
	public String showTip(Integer tipId, Model model) {
		logger.info("tipId = " + tipId);
		TipVO tipVo = tipService.findById(tipId);
		model.addAttribute("tip", tipVo);
		List<Reply> replies = replyService.findByTip(tipId);
		model.addAttribute("replies", replies);
		return "tipContent";
	}
}
